/**
 * 
 */
package org.dragonfire.dasm.instr.rules;

import org.dragonfire.dasm.instr.Rule;
import org.dragonfire.dasm.postprocessor.Postprocessor;

/**
 * @author Darth Android
 * @date Jul 21, 2007
 */
public class REL8Rule extends Rule {

	public String evalParam(Postprocessor post, String bits) {
		int val = Integer.parseInt(bits, 2);
		val -= post.getPC();
		String newBits = Integer.toBinaryString(val);
		while (newBits.length() < bits.length()) {
			newBits = val < 0 ? "1" : "0" + newBits;
		}
		boolean truncated = false;
		while (newBits.length() > 8) {
			if (newBits.charAt(0) != (val < 0 ? '1' : '0'))
				truncated = true;
			newBits = newBits.substring(1);
		}
		if (truncated)
			post.getCompiler().warn("WARN_TRUNCATED_DATA", val, "???");
		return newBits;
	}
}
